package com.gimi.cloud.bbp.client.service.mall.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gimi.cloud.bbp.client.service.mall.MallCartService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dao.dao.mall.MallCartDao;
import com.gimi.cloud.bbp.dao.dao.prod.EcProdCatModelValueDao;
import com.gimi.cloud.bbp.dao.dao.product.ProductSkuDao;
import com.gimi.cloud.bbp.dto.mall.MallCartDTO;
import com.gimi.cloud.bbp.entity.mall.MallCartEntity;
import com.gimi.cloud.bbp.entity.product.ProductSkuEntity;

import tk.mybatis.mapper.entity.Example;

/**
 * 购物车服务实现
 * @author lilinfeng
 */
@Service
public class MallCartServiceImpl implements MallCartService {
	@Autowired private MallCartDao mallCartDao;
	@Autowired private ProductSkuDao productSkuDao;
	@Autowired private EcProdCatModelValueDao prodCatModelValueDao;
	
	@Override
	public Response addGoodsToShoppingCart(MallCartDTO mallCart) {
		saveShoppingCart(mallCart,ADD);
		return new Response(ResponseCodeEnum.SUCCESSED,"成功添加商品到购物车");
	}

	@Override
	public Response updateShoppingCart(MallCartDTO mallCart) {
		saveShoppingCart(mallCart,MODIFY);
		return new Response(ResponseCodeEnum.SUCCESSED);
	}
	
	@Override
	public Integer saveShoppingCart(MallCartDTO mallCart,String instructions) {
		Example example = new Example(MallCartEntity.class);
		example.createCriteria().andEqualTo("userId", mallCart.getUserId())
			.andEqualTo("skuId", mallCart.getSkuId()).andEqualTo("isDelete", 0);
		List<MallCartEntity> selectByExample = mallCartDao.selectByExample(example);
		ProductSkuEntity sku = productSkuDao.selectByPrimaryKey(mallCart.getSkuId());
		if (sku==null) {
			throw new MsgException(ResponseCodeEnum.SERVICE_ERROR,"商品数据异常");
		}
		MallCartEntity mallCartEntity;
		if (selectByExample!=null&&selectByExample.size()>0) {
			mallCartEntity = selectByExample.get(0);
		}else {
			mallCartEntity = new MallCartEntity();
			mallCartEntity.setUserId(mallCart.getUserId());
			mallCartEntity.setSkuId(mallCart.getSkuId());
			mallCartEntity.setCartNum(0);
			mallCartEntity.setProdId(sku.getProdId());
			mallCartEntity.setCartSkuPrice(sku.getSkuPrice());
			mallCartEntity.setIsDelete(0);
			mallCartDao.baseInsert(mallCartEntity);
		}
		
		//根据指令更改商品数量
		Integer cartNum = mallCart.getCartNum();
		cartNum = cartNum==null||cartNum<1?1:cartNum;
		switch (instructions) {
		case ADD:mallCartEntity.setCartNum(mallCartEntity.getCartNum()+cartNum);break;
		case MODIFY:mallCartEntity.setCartNum(cartNum);break;
		}
		mallCartDao.baseUpdateByPrimaryKey(mallCartEntity);
		return mallCartEntity.getCartId();
	}

	@Override
	public Response showShoppingCart(MallCartDTO mallCart) {
		Response response = new Response(ResponseCodeEnum.SUCCESSED);
		List<MallCartEntity> mallCartDetailInfor = mallCartDao.selectMallCartDetailInfor(mallCart.getUserId(),null,null);
		
		//查询单品对应模板属性值
		if (mallCartDetailInfor!=null) {
			for (MallCartEntity mallCartEntity : mallCartDetailInfor) {
				try {
					ProductSkuEntity prodSku = mallCartEntity.getProdSku();
					String skuValue = prodSku.getSkuValue();
					if (StringUtils.isNotBlank(skuValue)) {
						prodSku.setCatModelValueEntities(prodCatModelValueDao.getModelIdByIds(skuValue));
					}
				} catch (Exception e) {
					throw new MsgException(ResponseCodeEnum.SERVICE_ERROR,"商品数据异常");
				}
			}
		}
		
		response.setRes_data(mallCartDetailInfor);
		return response;
	}

	@Override
	public Response deleteShoppingCart(JSONObject mallCart) {
		JSONArray cartIdList = mallCart.getJSONArray("cartIdList");
		deleteShoppingCartByCartIdList(cartIdList);
		return new Response(ResponseCodeEnum.SUCCESSED,"删除成功");
	}

	@Override
	public int deleteShoppingCartByCartIdList(List<?> cartIdList) {
		Example example = new Example(MallCartEntity.class);
		example.createCriteria().andIn("cartId", cartIdList);
		
		MallCartEntity record = new MallCartEntity();
		record.setIsDelete(1);
		
		return mallCartDao.updateByExampleSelective(record, example);
	}
}